package com.fzhucc.modules.lesson.service.impl;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.lesson.domain.Lesson;
import com.fzhucc.modules.lesson.domain.LessonFirstChapter;
import com.fzhucc.modules.lesson.domain.dto.LessonFirstChapterDto;
import com.fzhucc.modules.lesson.domain.dto.LessonFirstChapterQueryCriteria;
import com.fzhucc.modules.lesson.repository.LessonFirstChapterRepository;
import com.fzhucc.modules.lesson.repository.LessonRepository;
import com.fzhucc.modules.lesson.service.LessonFirstChapterService;
import com.fzhucc.modules.lesson.service.mapstruct.LessonFirstChapterMapper;
import com.fzhucc.utils.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @description 服务实现
* @author yyg
* @date 2022-12-07
**/
@Service
@RequiredArgsConstructor
public class LessonFirstChapterServiceImpl implements LessonFirstChapterService {

    private final LessonFirstChapterRepository lessonFirstChapterRepository;
    private final LessonFirstChapterMapper lessonFirstChapterMapper;

    private final LessonRepository lessonRepository;

    @Override
    public Map<String,Object> queryAll(LessonFirstChapterQueryCriteria criteria, Pageable pageable){
        Page<LessonFirstChapter> page = lessonFirstChapterRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<LessonFirstChapterDto> res = page.map(lessonFirstChapterMapper::toDto);
        List<LessonFirstChapterDto> list = res.getContent();
        for (LessonFirstChapterDto lessonFirstChapterDto : list) {
            Optional<Lesson> result = lessonRepository.findById(lessonFirstChapterDto.getLessonId());
            if (result.isPresent()){
                lessonFirstChapterDto.setLesson(result.get());
            }
        }
        return PageUtil.toPage(res);
    }

    @Override
    public List<LessonFirstChapterDto> queryAll(LessonFirstChapterQueryCriteria criteria){
        return lessonFirstChapterMapper.toDto(lessonFirstChapterRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LessonFirstChapterDto findById(Integer id) {
        LessonFirstChapter lessonFirstChapter = lessonFirstChapterRepository.findById(id).orElseGet(LessonFirstChapter::new);
        ValidationUtil.isNull(lessonFirstChapter.getId(),"LessonFirstChapter","id",id);
        return lessonFirstChapterMapper.toDto(lessonFirstChapter);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LessonFirstChapterDto create(LessonFirstChapter resources) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("用户ID未找到");
        }
        Optional<Lesson> res = lessonRepository.findById(resources.getLessonId());
        if (!res.isPresent()) {
            throw new BadRequestException("课程未找到");
        }
        return lessonFirstChapterMapper.toDto(lessonFirstChapterRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LessonFirstChapter resources) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("用户ID未找到");
        }
        LessonFirstChapter lessonFirstChapter = lessonFirstChapterRepository.findById(resources.getId()).orElseGet(LessonFirstChapter::new);
        ValidationUtil.isNull( lessonFirstChapter.getId(),"LessonFirstChapter","id",resources.getId());
        lessonFirstChapter.copy(resources);
        lessonFirstChapterRepository.save(lessonFirstChapter);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            lessonFirstChapterRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LessonFirstChapterDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LessonFirstChapterDto lessonFirstChapter : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程id", lessonFirstChapter.getLessonId());
            map.put("内容", lessonFirstChapter.getUrl());
            map.put("分类 视频、文档、图片", lessonFirstChapter.getType());
            map.put("名称", lessonFirstChapter.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}